package behavioralPatterns.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

interface Iterator<T> {
    boolean hasNext();

    T next();
}

class ConcreateIterator<T> implements Iterator<T> {
    private List<T> list;
    private int index;

    public ConcreateIterator(List<T> list) {
        this.list = list;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        return index < list.size();
    }

    @Override
    public T next() {
        if (hasNext()) {
            return list.get(index++);
        }
        return null;
    }
}

interface Container<T> {
    Iterator<T> createIterator();
}

class ConcreateContainer<T> implements Container<T> {
    private List<T> list;

    public ConcreateContainer(List<T> list) {
        this.list = list;
    }

    @Override
    public Iterator<T> createIterator() {
        return new ConcreateIterator<>(list);
    }
}

public class IteratorPattern {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        Container<Integer> container = new ConcreateContainer<>(numbers);
        Iterator<Integer> iterator = container.createIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
